/*package com.uit.smarttool.activity;

import java.util.ArrayList;

import com.uit.smarttool.activity.R;
import com.uit.smarttool.ultility.CallSoap;
import com.uit.smarttool.ultility.CustomCategories;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public final class Tab2Activity extends Activity {

	public static String result;
	CustomCategories adapter;
	ListView lvloaisp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layoutcategories);
		lvloaisp = (ListView) findViewById(R.id.lvCategories);
		try {
			result = "START";
			ConnectWebService c = new ConnectWebService();
			c.join();
			c.start();
			while (result == "START") {
				try {
					Thread.sleep(10);
				} catch (Exception ex) {
				}
			}
			ArrayList<String> list = c.listloaisp;
			adapter = new CustomCategories(this, list);
			lvloaisp.setAdapter(adapter);
		} catch (Exception e) {
		}
	}

	// thread for loading data to arraylist
	private class ConnectWebService extends Thread {
		
		private CallSoap cs;
		private ArrayList<String> listloaisp;
		public void run() {
			try {
				cs = new CallSoap();
				listloaisp = cs.getDanhMucSanPham();
				Tab2Activity.result = "Finish";
			} catch (Exception ex) {
				Tab2Activity.result = ex.toString();
			}

		}
	}
}
*/

package com.uit.smarttool.activity;

import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.client.android.result.TextResultHandler;
import com.uit.smarttool.dto.SanPham;
import com.uit.smarttool.ultility.CallSoap;
import com.uit.smarttool.ultility.ImageLoader;
import com.uit.smarttool.ultility.LazyAdapter;

public final class Tab2Activity extends Activity{

		
	ListView list;
    LazyAdapter adapter;
    Activity activity;
    
    public static List<SanPham> tbproduct;
    public String[] lvproduct;
	public String[] lvProductImage;
	public String[] lvProductDongia;
    
    public static int maLoaiSP;
	public static String maSP;
	public static Boolean flag = false;	
		
	public static String result = "";
	public TextView productInfo;
	
	public ImageLoader imageLoader; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_main);
        activity = this;
        list=(ListView)findViewById(R.id.list);
        adapter = new LazyAdapter(this, LoginActivity.lvImageCategory, LoginActivity.lvDanhmucsp, LoginActivity.temp);
        list.setAdapter(adapter);
        
        list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
			{				
				
				// TODO Auto-generated method stub
				if(flag == false)
				{
					if(!LoginActivity.lvDanhmucsp[arg2].equals("Mặc định"))
					{
						maLoaiSP = Integer.valueOf(LoginActivity.tbDanhmucsanpham.get(arg2).getMaLoaiSP());
						
						showProduct s = new showProduct();
						try {
							result = "START";
							s.join();
							s.start();
							while (result == "START") {
								try {
									Thread.sleep(10);
								} catch (Exception ex) {
								}
							}
						} catch (Exception e) {
						}
						lvproduct = new String[tbproduct.size()];
						lvProductImage = new String[tbproduct.size()];	
						lvProductDongia = new String[tbproduct.size()];
						for (int i = 0; i < tbproduct.size(); i++) 
						{
							lvproduct[i] = tbproduct.get(i).getTenSP();
							lvProductImage[i] = tbproduct.get(i).getURL();
							lvProductDongia[i] = tbproduct.get(i).getDonGia().substring(0, tbproduct.get(i).getDonGia().length() - 5);
						}
						
						adapter = new LazyAdapter(activity, lvProductImage, lvproduct, lvProductDongia);
						list.setAdapter(adapter);
						flag = true;
					}
				}
				else 
					if(!lvproduct[arg2].equals("Mặc định"))
					{
						maSP = tbproduct.get(arg2).getMaSP();
						showDialog(lvproduct[arg2] + ":" + tbproduct.get(arg2).getGioiThieu(), lvProductImage[arg2]);
					}
				

			}

		});
        
        Button back =(Button) findViewById(R.id.button_back);
        back.setOnClickListener(listener);
    }
    
    @Override
    public void onDestroy()
    {
        list.setAdapter(null);
        super.onDestroy();
    }
    
    public OnClickListener listener=new OnClickListener(){
        @Override
        public void onClick(View arg0) {
        	
        	adapter = new LazyAdapter(activity, LoginActivity.lvImageCategory, LoginActivity.lvDanhmucsp, LoginActivity.temp);
            list.setAdapter(adapter);
            flag = false;
            //adapter.imageLoader.clearCache();
            adapter.notifyDataSetChanged();
        }
    };
    
    public void showDialog(String item, String imageURL) {
		final Dialog dialog = new Dialog(this);
		dialog.setContentView(R.layout.dialogmain);
		dialog.setTitle("Thông tin sản phẩm");
		
		productInfo = (TextView) dialog.findViewById(R.id.text_view_dialog_body);
		productInfo.setText(item);
		
		ImageView image = (ImageView) dialog.findViewById(R.id.logo_thum);
		imageLoader = new ImageLoader(activity.getApplicationContext());
		imageLoader.DisplayImage(imageURL, image);
		/*URL url;
		try {
			url = new URL(imageURL);
			Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
			image.setImageBitmap(bmp);
		} catch (MalformedURLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	*/
		
		
		Button btAdd = (Button) dialog.findViewById(R.id.button_dialog_add);
		Button btCancel = (Button) dialog.findViewById(R.id.button_dialog_cancel);				
		
		dialog.show();
		btAdd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				insertCTGioHang ic = new insertCTGioHang();
				try {
					result = "START";
					ic.join();
					ic.start();
					while (result == "START") {
						try {
							Thread.sleep(10);
						} catch (Exception ex) {
						}
					}
				} catch (Exception e) {
					Toast.makeText(getApplicationContext(), "Thêm vào giỏ hàng thất bại", 2000).show();
				}
				Toast.makeText(getApplicationContext(), "Thêm vào giỏ hàng thành công", 2000).show();
				dialog.cancel();
			}
				
		});

		btCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.cancel();
			}
		});
	}
    
    private class showProduct extends Thread {

		public void run() {
			try {
				CallSoap cs = new CallSoap();
				Tab2Activity.tbproduct = cs.getDanhSachSanPhamTheoMuc(maLoaiSP);
				Tab2Activity.result = "Finish";
			} catch (Exception ex) {
				Tab1Activity.resultDelete = ex.toString();
			}

		}
	}
    
    // thread for inserting data to db
 	private class insertCTGioHang extends Thread {
 			
 			private CallSoap cs;

 			public void run() {
 				try {
 					cs = new CallSoap();
 					cs.insertCTGioHang(String.valueOf(Tab1Activity.MaGH), maSP, 1);

 					Tab2Activity.result = "Finish";

 				} catch (Exception ex) {
 					TextResultHandler.result = ex.toString();
 				}
 			}
 		}
    

}
